#include "../main.h"

Map *router_msgmap;
//添加原始消息
void append_router_data(int sockfd, char *msg, int len)
{
    if (len > 0)
    {
        if (router_msgmap == NULL)
        {
            router_msgmap = (Map *)malloc(sizeof(Map));
            router_msgmap->head = NULL;
        }
        char *data = charCopy(msg, len);
        addToMap(router_msgmap, sockfd, data, len);
        MNode *curNode = getbykey(router_msgmap, sockfd);
        RecvData *recvData = curNode->record->recvData;
        if (curNode->record->recvData->length > 0)
        {
            int get_head = get_router_head(recvData);

            if (get_head == 1)
            {
                process_router_msg(curNode);
            }
        }
        free(data);
        data = NULL;
    }
}

void process_router_msg(MNode *curNode)
{
    RecvData *recvData = curNode->record->recvData;
    if (curNode->record->recvData->length == 0)
    {
        return;
    }
    int get_head = get_router_head(recvData);
    while (get_head)
    {
        usleep(1000);
        tidy_router_msg(recvData);
        int msgsize = get_size(recvData->data + 3 * sizeof(int));
        if (recvData->length >= msgsize + 4 * sizeof(int))
        {
            char *buffer = (char *)malloc(msgsize + 1);
            memset(buffer, 0, msgsize + 1);
            memcpy(buffer, recvData->data + 4 * sizeof(int), msgsize);
            buffer[msgsize] = '\0';
            analyze_msg(curNode->record->sockfd, buffer, msgsize + 1);
            free(buffer);
            buffer = NULL;
            int bodysize = msgsize + 4 * sizeof(int);
            int leftsize = recvData->length - bodysize;
            if (leftsize > 0)
            {
                char *lbuffer = (char *)malloc(leftsize);
                memcpy(lbuffer, recvData->data + bodysize, leftsize);
                free(recvData->data);
                recvData->data = NULL;
                recvData->data = charCopy(lbuffer, leftsize);
                recvData->length = leftsize;
                free(lbuffer);
                lbuffer = NULL;
            }
            else
            {
                free(recvData->data);
                recvData->data = NULL;
                recvData->length = 0;
            }
        }
        else
        {
            OUT_DEBUG(DEBUG, "#########can not parse node total:%d,real length:%d", msgsize + 16, recvData->length);
            break;
        }
        get_head = get_router_head(recvData);
        if (get_head == 0)
        {
            break;
        }
    }
}

int get_router_head(RecvData *recvData)
{
    if (recvData->length > 0)
    {
        int a = 0;
        for (a = 0; a < recvData->length - 8; a++)
        {
            if (recvData->data[a] == 0x76)
            {
                if (recvData->data[a + 4] == 0x01 && recvData->data[a + 8] == 0x01)
                {
                    return 1;
                    break;
                }
            }
        }
    }
    return 0;
}

void tidy_router_msg(RecvData *recvData)
{
    if (recvData->length > 0)
    {
        int a = 0;
        int result = 0;

        for (a = 0; a < recvData->length - 8; a++)
        {
            if (recvData->data[a] == 0x76)
            {
                if (recvData->data[a + 4] == 0x01 && recvData->data[a + 8] == 0x01)
                {
                    result = 1;
                    break;
                }
            }
        }
        if (result > 0)
        {
            if (a == 3)
            {
                return;
            }
            else
            {
                int leftsize = recvData->length - a + 3;
                char *lbuffer = (char *)malloc(leftsize);
                memset(lbuffer, 0, leftsize);
                memcpy(lbuffer + 3, recvData->data + a, recvData->length - a);
                free(recvData->data);
                recvData->data = NULL;
                recvData->data = lbuffer;
                recvData->length = leftsize;
                printf("aindex:%d\n,leftsize:%d,head:%02x\n", a, leftsize, recvData->data[3]);
            }
        }
    }
}
